//
//  LuckyMainVC.m
//  SMTools
//
//  Created by fengsh on 2018/12/25.
//  Copyright © 2018年 fengsh. All rights reserved.
//

#import "LuckyMainVC.h"
#import "LuckyItemCell.h"
#import <Masonry/Masonry.h>
#import "LuckyReadyVC.h"
#import "LuckySettingWnd.h"
#import "LuckyMembersVC.h"
#import "LuckyResultVC.h"
#import "ScencMainVC.h"
#import <Masonry/Masonry.h>

@interface LuckyMainVC ()<NSSplitViewDelegate,NSTableViewDelegate,NSTableViewDataSource,NSTabViewDelegate>
@property (weak) IBOutlet NSSplitView *frame_split;
@property (weak) IBOutlet NSTableView *sidemenu;
///带上下split的右侧内容区。
@property (weak) IBOutlet NSView *right_contentview;
///普通内容区(样式一)
@property (weak) IBOutlet NSView *right_view;
@property (weak) IBOutlet NSSplitView *right_subsplit;
@property (strong) NSMutableArray                   *titles;

@property (nonatomic, strong) LuckyReadyVC                          *readyvc;
@property (nonatomic, strong) LuckySettingWnd                       *settingwnd;
@property (nonatomic, strong) LuckyMembersVC                        *membervc;
@property (nonatomic, strong) LuckyResultVC                         *resultvc;
@property (nonatomic, strong) ScencMainVC                           *scencvc;

@property (weak) IBOutlet NSTabView *tabs;

@end

@implementation LuckyMainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    ///初始大小
    [self.frame_split setPosition:[self.frame_split maxPossiblePositionOfDividerAtIndex:0] ofDividerAtIndex:0];
    [self.right_subsplit setPosition:[self.right_subsplit minPossiblePositionOfDividerAtIndex:0] ofDividerAtIndex:0];
    [self.sidemenu.headerView setHidden:YES];
    NSArray *cols = self.sidemenu.tableColumns;
    NSTableColumn *tcol = cols[0];
    float w = 200;
    tcol.width = w;
    
    self.sidemenu.headerView = nil;
    [self.sidemenu registerNib:[[NSNib alloc] initWithNibNamed:@"LuckyItemCell" bundle:nil] forIdentifier:@"function_clr"];
    
    NSArray *title = @[@"奖项",@"获奖列表",@"活动总成员列表"];
    self.titles = [NSMutableArray arrayWithArray:title];
    
    [self.sidemenu reloadData];
}

- (void)dealloc
{
    NSLog(@"Main Free.");
}

#pragma mark - tableview
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return self.titles.count;
}

- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
    return 50;
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{

    NSUserInterfaceItemIdentifier idf = tableColumn.identifier;
    LuckyItemCell *cv = [tableView makeViewWithIdentifier:idf owner:self];
    cv.frame = NSMakeRect(0, 0, tableColumn.width, 50);
    cv.lb_title.stringValue = self.titles[row];
    NSView *line = [[NSView alloc]init];
    [line setWantsLayer:YES];
    line.layer.backgroundColor = [NSColor gridColor].CGColor;
    [line setNeedsDisplay:YES];
    [cv addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(0));
        make.leading.equalTo(@(10));
        make.height.equalTo(@(1));
        make.trailing.equalTo(@(0));
    }];
    return cv;
}

- (nullable id)tableView:(NSTableView *)tableView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
{
    return nil;
}

#pragma mark - delegate
- (void)splitView:(NSSplitView *)splitView resizeSubviewsWithOldSize:(NSSize)oldSize
{
    if (splitView == self.frame_split) {
        ///固定左边大小
        CGFloat dividerThickness = [splitView dividerThickness];
        NSRect leftRect = [[[splitView subviews] objectAtIndex:0] frame];
        NSRect rightRect = [[[splitView subviews] objectAtIndex:1] frame];
        NSRect newFrame = [splitView frame];
        
        leftRect.size.height = newFrame.size.height;
        leftRect.origin = NSMakePoint(0, 0);
        rightRect.size.width = newFrame.size.width - leftRect.size.width
        - dividerThickness;
        rightRect.size.height = newFrame.size.height;
        rightRect.origin.x = leftRect.size.width + dividerThickness;
        
        [[[splitView subviews] objectAtIndex:0] setFrame:leftRect];
        [[[splitView subviews] objectAtIndex:1] setFrame:rightRect];
        
    } else {
        //只需要适配大小
        [splitView adjustSubviews];
    }
}

- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex
{
    if (splitView == self.frame_split && dividerIndex == 0) {
        return 60.0f;
    } else if (splitView == self.right_subsplit && dividerIndex == 0) {
        return 200.0f;
    }
    return proposedMinimumPosition;
}

- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex
{
    if (splitView == self.frame_split && dividerIndex == 0) {
        return 200.0f;
    } else if (splitView == self.right_subsplit && dividerIndex == 0) {
        return  CGRectGetHeight(self.view.frame);
    }
    return proposedMaximumPosition;
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification {
    
    NSTableView *tableView = notification.object;
    if ([tableView isKindOfClass:[NSTableView class]]) {
        NSInteger row = tableView.selectedRow;
//        NSString *title = self.titles[row];
        switch (row) {
            case 0:
            {
                self.right_contentview.hidden = YES;
                self.right_view.hidden = NO;
                if (!self.readyvc) {
                    self.readyvc = [[LuckyReadyVC alloc]init];
                }
                [self.right_view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                [self.right_view addSubview:self.readyvc.view];
                [self.readyvc.view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.edges.equalTo(@(0));
                }];
                
            }
                break;
            case 1:
            {
                self.right_contentview.hidden = YES;
                self.right_view.hidden = NO;
                
                if (!self.resultvc) {
                    self.resultvc = [[LuckyResultVC alloc]init];
                }
                
                [self.right_view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                [self.right_view addSubview:self.resultvc.view];
                [self.resultvc.view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.edges.equalTo(@(0));
                }];
            }
                break;
                case 2:
            {
                self.right_contentview.hidden = YES;
                self.right_view.hidden = NO;
                if (!self.membervc) {
                    self.membervc = [[LuckyMembersVC alloc]init];
                }
                [self.right_view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                [self.right_view addSubview:self.membervc.view];
                [self.membervc.view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.edges.equalTo(@(0));
                }];
                
            }
                break;
            default:
            {
                self.right_contentview.hidden = NO;
                self.right_view.hidden = YES;
            }
                break;
        }
    }
}

- (IBAction)onAddActiveClicked:(id)sender
{
    ///不要用init,有nib要用nib的方式，否则显示不了。
    BOOL visable = [self.settingwnd.window isVisible];
    if (!visable) {
        self.settingwnd = nil; //free 之前的。
    } else { //显示了就不产生新的窗口了.
        [self.settingwnd.window orderFront:nil];
        return;
    }
    
    self.settingwnd = [[LuckySettingWnd alloc]initWithWindowNibName:@"LuckySettingWnd"];
    [self.settingwnd.window center];
    ///不要使用orderfront,因为出来时焦点不是在该window上的。
    visable = [self.settingwnd.window isVisible];
    [self.settingwnd showWindow:nil];
}

- (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
{
    if ([tabViewItem.identifier isEqualToString:@"scene"]) {
        if (!self.scencvc) {
            self.scencvc = [[ScencMainVC alloc]init];
            [tabViewItem.view addSubview:self.scencvc.view];
            
            [self.scencvc.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(@(0));
            }];
        }
    }
}

@end
